package org.dromara.web.controller;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.web.domain.vo.TableDDL;
import org.dromara.web.domain.vo.TableDiffVo;
import org.dromara.web.service.TableService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 数据库对比
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/table")
public class TableController {

    private final TableService tableService;

    /**
     * 表差异
     *
     * @return 表差异信息
     */
    @GetMapping("/diff")
    public R<TableDiffVo> tableDiff() {
        return R.ok(tableService.tableDiff());
    }

    /**
     * 根据表名单个ddl
     *
     * @param tableName 表名
     * @return ddl信息
     */
    @GetMapping("/ddl")
    public R<List<TableDDL>> tableDDL(@RequestParam("tableName") String tableName) {
        return R.ok(tableService.tableDDL(tableName));
    }

    /**
     * ddl 之间差异
     *
     * @return ddl差异信息
     */
    @GetMapping("/ddl/diff")
    public R<Map<String, List<String>>> ddlDiff(@RequestParam("normal") boolean normal) {
        return R.ok(tableService.ddlDiff(normal));
    }

    /**
     * 数据反向对比
     * <p>
     * 数据源1是参考库,改动数据库2
     *
     * @return 数据对比SQL
     */
    @GetMapping("/data/diff")
    public R<List<String>> dataDiff(@RequestParam("prefix") String prefix, @RequestParam("normal") boolean normal) {
        return R.ok(tableService.dataDiff(prefix, normal));
    }

}
